/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: serializer.h
 *
 * Purpose: define serialization base class
 *
 * Developer:
 *   wen.gu , 2020-04-22
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_COM_RUNTIME_SERIALIZER_H__
#define __ARA_COM_RUNTIME_SERIALIZER_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include <cstdint>
#include <type_traits>
#include <memory>

#include "ara/core/optional.h"
#include "ara/core/string.h"
#include "ara/core/vector.h"
#include "ara/core/array.h"
#include "ara/com/runtime/runtime_types.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace com
{
namespace runtime
{

struct SerializerPayload
{
    std::shared_ptr<uint8_t> data;
    uint32_t size;
};
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

/**class for serializer */
class Serializer
{
    template<typename T>
    using Optional = ara::core::Optional<T>;

    template<typename T>
    using Vector = ara::core::Vector<T>;

    template<typename T, std::size_t N>
    using Array = ara::core::Array<T, N>;

    using String = ara::core::String;

public:
    using PayloadPtr = std::shared_ptr<uint8_t>;
public:
    Serializer() = default;
    virtual ~Serializer() = default;

public:
    /** get payload */
    virtual PayloadPtr GetPayload(uint32_t& size) = 0;

public:
    virtual void Write(bool val) = 0;
    virtual void Write(int8_t val) = 0;
    virtual void Write(int16_t val) = 0;
    virtual void Write(int32_t val) = 0;
    virtual void Write(int64_t val) = 0;
    virtual void Write(uint8_t val) = 0;
    virtual void Write(uint16_t val) = 0;
    virtual void Write(uint32_t val) = 0;
    virtual void Write(uint64_t val) = 0;
    virtual void Write(float val) = 0;
    virtual void Write(double val) = 0;
    virtual void Write(const String& val) = 0;

public:
    template<typename T > /**todo, is need check the type of T */
    void Write(const Optional<T>& val)
    {/** treat Optional as dynamic array, |element count(0 or 1)| element value| */
        if (val.has_value())
        {
            Write((uint32_t)1);
            Write(*val);
        }
        else
        {
            Write(uint32_t(0));
        }
    }

    template<typename T>
    void Write(const Vector<T>& val)
    {/** treat Optional as dynamic array, |val.size()| element values...| */
        if (val.size() > 0)
        {
            Write((uint32_t)val.size());
            for (auto it : val)
            {
                Write(it);
            }
        }
        else
        {
            Write(uint32_t(0));
        }
    }

    template<typename T, std::size_t N>
    void Write(const Array<T, N>& val)
    {
        if (val.size() > 0) /**here array not fully fill */
        {
            Write((uint32_t)val.size());
            for (auto it : val)
            {
                Write(it);
            }
        }
        else
        {
            Write(uint32_t(0));
        }
    }

    template<typename T, class=typename std::enable_if<std::is_enum<T>::value >::type >
    void Write(T val) /** write for enum */
    {
        Write((typename internal_impl::enum2Int<sizeof(T)>::type)val);
    }

    template<typename T, class=typename std::enable_if<internal_impl::has_member_serialize<T, const Serializer&>::value >::type >
    void Write(const T& val) /** write for struct */
    {/** every struct need provide a template method: 
      * template<typename T>
      * Serialize(const T& ser) const; 
      */
        val.Serialize(*this); /**todo, refineme */
    }
};


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

using SerializerPtr = std::shared_ptr<Serializer>;

} /** namespace runtime */
} /** namespace com */
} /** namespace ara */

#endif /** !__ARA_COM_RUNTIME_SERIALIZER_H__ */

